package com.chucang.shucang.ttp.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.chucang.shucang.common.base.utils.R;
import com.chucang.shucang.ttp.service.AliPayService;
import com.chucang.shucang.ttp.vo.PayInfoVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author flitsneak
 * @email flitsneak@gmail.com
 * @date 2022/10/23 0:35
 * @description
 */
@RestController
@RequestMapping("/alipay")
@RequiredArgsConstructor
public class AliPayController {
    private final AliPayService aliPayService;

    @PostMapping("/payForOrder")
    public R<AlipayTradeAppPayResponse> payForOrder(@RequestBody PayInfoVO payInfoVO) throws AlipayApiException {
        AlipayTradeAppPayResponse appPayResponse = aliPayService.payForOrder(payInfoVO);
        if (Objects.nonNull(appPayResponse)) {
            return R.ok(appPayResponse);
        } else {
            return R.failed();
        }
    }
}
